package address

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/controllers/login"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type DeleteAddress struct {
	beego.Controller
}

func (c *DeleteAddress) DeleteAddress() {

	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/address/DeleteAddress"
	//res := resMes.GetAdminUser{}
	//user := []*Admin.User{}
	log := &error2.Log{}
	mes := &error2.Mes{}
	var err error
	o := orm.NewOrm()

	id := c.GetString("id")
	if id == "" {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "400", "idNull")
		return
	}

	err, openid := login.CheckOpenid(c.Ctx.Request.Header["Openid"])
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	//fmt.Println(key)

	sql := "delete from `address` where `address_id`=? and `user_id`=?"
	sqlRes, err := o.Raw(sql, id, openid).Exec()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	num, err := sqlRes.RowsAffected()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	if num == 0 {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "400", "未找到或无权限")
		return
	}

	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes
}
